package com.xt.LibaryDeskManager.servlet;

import com.xt.LibaryDeskManager.dto.SelectSeatDto;
import com.xt.LibaryDeskManager.service.SelectSeatService;
import com.xt.LibaryDeskManager.utils.StrUtils;
import com.xt.LibaryDeskManager.vo.SelectSeatVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import java.io.IOException;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/selectSeat")
public class SelectSeatController {
    @Autowired
    private SelectSeatService selectSeatService;

    /**
     * 查找所有预约信息
     * @param page
     * @param limit
     */
    @ResponseBody
    @RequestMapping("/getAllSelectSeat")
    public Map getAllSelectSeat(Integer page, Integer limit,SelectSeatVo selectSeatVo) throws Exception {
        return selectSeatService.getAllSelectSeat(page,limit,selectSeatVo);
    }

    /**
     * 根据时间查找
     */
    @ResponseBody
    @RequestMapping("/selectByTimeCode")
    public Map selectByTimeCode(String begin1,String seatNo)throws Exception{
        int code=0;
        List<SelectSeatDto> list= selectSeatService.selectByTime(begin1,seatNo);
        if (list.size()!=0){
            for (SelectSeatDto s : list) {
                if (s.getStatue()!=3&&s.getStatue()!=4){
                    code+=s.getBegin2();
                }
            }
        }
       return StrUtils.creatMessage("code",code);
    }

    /**
     * 根据时间查找
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/selectByTime")
    public Map selectByTime(String begin1,String seatNo,Integer page,Integer limit) throws Exception {
        return selectSeatService.selectByTime(begin1, seatNo,page,limit);
    }

    /**
     * 查找指定学生在指定时间的选座记录
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/selectByTimeStudentNo")
    public Map selectByTimeStudentNo(String studentNo,String begin1,Integer begin2) throws Exception {
        int code=selectSeatService.selectByTimeStudentNo(studentNo,begin1,begin2);
        return StrUtils.creatMessage("code",code);
    }
    /**
     * 查找指定学生选座记录
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/selectSeatByStudentNo")
    public  Map selectSeatByStudentNo(String studentNo,Integer page,Integer limit) throws Exception {
        return selectSeatService.selectSeatByStudentNo(page,limit,studentNo);
    }

    /**
     * 添加预约记录
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/addSelectSeat")
    public Map addSelectSeat(SelectSeatVo selectSeatVo) throws Exception {
        int code= selectSeatService.addSelectSeat(selectSeatVo);
        return StrUtils.creatMessage("code",code);
    }

    /**
     * 手动释放座位
     * @throws ServletException
     * @throws IOException
     */
    @ResponseBody
    @RequestMapping("/updateSelectSeat")
    public Map updateSelectSeat(String seatNo,String selectId,Integer begin2)throws Exception{
        int code=selectSeatService.updateSelectSeat(seatNo,selectId,4,begin2);
        return StrUtils.creatMessage("code",code);
    }

    @ResponseBody
    @RequestMapping("/adminOperating")
    public Map adminOperating(Integer clock)throws Exception{
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DATE,calendar.get(Calendar.DATE)+1);
       String DateVlue=null;
        if (calendar.get(Calendar.MONTH)<9){
            DateVlue=String.valueOf(calendar.get(Calendar.YEAR))+"-0"+String.valueOf(calendar.get(Calendar.MONTH)+1)+"-"+String.valueOf(calendar.get(Calendar.DATE));
        }else{
            DateVlue=String.valueOf(calendar.get(Calendar.YEAR))+"-"+String.valueOf(calendar.get(Calendar.MONTH)+1)+"-"+String.valueOf(calendar.get(Calendar.DATE));
        }
        int code=0;
        if (clock==7){
            code=selectSeatService.updateSelectSeat(DateVlue,1,1,2);
        }else if (clock==12){
            code=selectSeatService.updateSelectSeat(DateVlue,1,2,3);
            selectSeatService.updateSelectSeat(DateVlue,2,1,2);
        }else if (clock==10){
            code=selectSeatService.updateSelectSeat(DateVlue,2,2,3);
        }
        return StrUtils.creatMessage("code",code);
    }
}
